﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.ProductionWorkOrders;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MangoMes.Write.API.Applications.Login;
using MediatR;

namespace MangoMes.Write.API.Applications.ProductionWorkOrders
{
    public class ProductionOrdersDeleteHander : IRequestHandler<ProductionOrdersDeleteCommand, ResultsDTO>
    {
        private readonly IBateRepository<ProductionOrders> pro;
        private readonly ILogger<ProductionOrdersDeleteHander> logger;
        private readonly Logincommonality _Logincommonality;

        public ProductionOrdersDeleteHander(IBateRepository<ProductionOrders> pro, ILogger<ProductionOrdersDeleteHander> logger, Logincommonality _Logincommonality)
        {
            this.pro = pro;
            this.logger = logger;
            this._Logincommonality = _Logincommonality;
        }

        public async Task<ResultsDTO> Handle(ProductionOrdersDeleteCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ResultsDTO res=new ResultsDTO() { Code=0};
                DapperHelper dapper = new DapperHelper();
                string sql = $"UPDATE productionworkorders SET State=2 WHERE ProductionWorkOrdersNo='{request.ProNo}'";
                res.Code = await pro.Add(sql, new ProductionOrders(), dapper.MangoMesWrite);
                logger.LogInformation("成功");
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError("失败"+ex);
                throw;
            }
        }
    }
}
